package com.example.studyenglish.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.studyenglish.R;
import com.example.studyenglish.database.DataAccess;

import java.util.ArrayList;
import java.util.HashMap;

public class Main extends Activity {
	AlertDialog menuDialog;
	ListView listView1;
	GridView menuGrid, bottomGrid;
	View menuView;

	/*-- MENU菜单选项下标 --*/
	private final int MENU_REFRESH = 0;// 刷新
	private final int MENU_QUIT = 1;// 退出
	private final int MENU_IMPORT = 2;// 倒入新词库
	private final int MENU_HELP = 3;// 帮助
	private final int MENU_ABOUT = 4;// 关于
	private final int MENU_DELETE = 5;// 删除词库

	/*-- 底部菜单选项下标--*/
	private final int BOTTOM_HOME = 0;// 首页
	private final int BOTTOM_BACK = 1;// 退后
	private final int BOTTOM_FORWARD = 2;// 前进
	private final int BOTTOM_RUSTY = 3;// 生词
	private final int BOTTOM_MENU = 4;// 菜单

	// listView 菜单
	private final int LISTVIEW_BOOKSELECT = 0;// 选择词库
	private final int LISTVIEW_STUDY = 1;// 学习模式
	private final int LISTVIEW_REVIEW = 2;// 复习模式
	private final int LISTVIEW_TEST = 3;// 测试模式
	private final int LISTVIEW_RUSTY = 4;// 生词库
	private final int LISTVIEW_DICTIONARY = 5;// 词典模式
	private final int LISTVIEW_SET=6;//设置

	/** LISTVIEW菜单图片 **/
	int[] menu_listview_image = { R.drawable.listview_pickbook,
			R.drawable.listview_study, R.drawable.listview_review,
			R.drawable.listview_test, R.drawable.listview_rusty,
			R.drawable.listview_dictionary, R.drawable.listview_set };
	/** LISTVIEW菜单文字 **/
	String[] menu_listview_name = { "选择词库", "学习模式", "复习模式", "测试模式", "生疏词汇",
			"电子词典", "设置" };

	/** 底部菜单图片 **/
	int[] bottom_image = { R.drawable.bottom_home, R.drawable.bottom_back,
			R.drawable.bottom_forward, R.drawable.bottom_rusty,
			R.drawable.bottom_menu };
	/** 底部菜单文字 **/
	String[] bottom_name = { "首页", "后退", "前进", "生词", "菜单" };

	/** GRIDVIEW菜单图片 **/
	int[] menu_grid_image = { R.drawable.menu_refresh, R.drawable.menu_quit,
			R.drawable.menu_import, R.drawable.menu_help,
			R.drawable.menu_about, R.drawable.menu_delete };
	/** GRIDVIEW菜单文字 **/
	String[] menu_grid_name = { "刷新", "退出", "新词库", "帮助", "关于", "删词库" };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// ListView列表
		listView1 = (ListView) findViewById(R.id.ListView1);
		listView1.setAdapter(getMenuAdapter(menu_listview_name,
				menu_listview_image));
		listView1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
				// TODO Auto-generated method stub
				switch (arg2) {
				case LISTVIEW_BOOKSELECT: {
					Intent i = new Intent(Main.this, BookSelect.class);
					startActivity(i);
					break;
				}
				case LISTVIEW_STUDY: {
					Intent i = new Intent(Main.this, Learning.class);
					startActivity(i);
					break;
				}
				case LISTVIEW_REVIEW: {
					Intent i = new Intent(Main.this, Review.class);
					startActivity(i);
					break;
				}
				case LISTVIEW_TEST: {
					Intent i = new Intent(Main.this, Exam.class);
					startActivity(i);
					break;
				}
				case LISTVIEW_RUSTY: {
					Intent i = new Intent(Main.this, Rusty.class);
					startActivity(i);
					break;
				}
				case LISTVIEW_DICTIONARY: {
					Intent i = new Intent(Main.this, Dictionary.class);
					startActivity(i);
					break;
				}
				case LISTVIEW_SET: {
					menuDialog.show();
					break;
				}
				}
			}
		});

		// 创建底部菜单
		bottomGrid = (GridView) findViewById(R.id.GridView_Bottom);
		bottomGrid.setBackgroundResource(R.drawable.bottom_background);// 设置背景
		bottomGrid.setNumColumns(5);// 设置每行列数
		bottomGrid.setGravity(Gravity.CENTER);// 位置居中
		bottomGrid.setVerticalSpacing(10);// 垂直间隔
		bottomGrid.setHorizontalSpacing(10);// 水平间隔
		bottomGrid.setAdapter(getMenuAdapter(bottom_name, bottom_image));// 设置菜单Adapter

		// 监听底部菜单选项
		bottomGrid.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
									long arg3) {
				switch (arg2) {
				case BOTTOM_HOME: {
					Toast.makeText(Main.this, "目前既是首页", Toast.LENGTH_SHORT)
							.show();
					break;
				}
				case BOTTOM_BACK: {
					Toast.makeText(Main.this, "目前既是首页", Toast.LENGTH_SHORT)
							.show();
					break;
				}
				case BOTTOM_FORWARD: {
					Intent i = new Intent(Main.this, BookSelect.class);
					startActivity(i);
					break;
				}
				case BOTTOM_RUSTY:
								  { Intent i=new
                                          Intent(Main.this,Rusty.class);
								  startActivity(i); 
								  break;}
								 
					
				case BOTTOM_MENU:
					menuDialog.show();
					break;
				}
			}
		});

		// 自定义menu菜单
		menuView = View.inflate(this, R.layout.main_bottom_menu, null);

		// 创建AlertDialog
		menuDialog = new AlertDialog.Builder(this).create();
		menuDialog.setView(menuView);
		menuDialog.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
								 KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_MENU)// 监听按键bottom_menu
				{
					dialog.dismiss(); // 弹出对话窗口
				}
				return false;
			}
		});

		menuGrid = (GridView) menuView.findViewById(R.id.gridview);
		menuGrid.setAdapter(getMenuAdapter(menu_grid_name, menu_grid_image));

		// 监听menuGrid选项
		menuGrid.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
									long arg3) {
				switch (arg2) {
				case MENU_REFRESH:// 刷新重置词库
				{
				Dialog dialog = new AlertDialog.Builder(Main.this)
							.setTitle("重置当前词库")
							.setMessage("确定要将这个词库重置吗？它将失去所有学习记录")
							.setPositiveButton("确定",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
											DataAccess data = new DataAccess(
													Main.this);
											data.ResetBook();
											Toast.makeText(Main.this,
													"该词库已被重置",
													Toast.LENGTH_SHORT).show();

										}
									})
							.setNegativeButton("取消",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
										}
									}).create();
					dialog.show();
					break;
				}
				case MENU_QUIT:// 退出程序
				{
					System.exit(0);
					break;
				}
				case MENU_IMPORT://
				{
					Intent i = new Intent(Main.this, BookImport.class);
					startActivity(i);
					break;
				}
				case MENU_HELP:// 帮助
				{
					Intent i = new Intent(Main.this, MenuHelp.class);
					startActivity(i);
					break;
				}

				case MENU_ABOUT:// 关于
				{
					Intent i = new Intent(Main.this, MenuAbout.class);
					startActivity(i);
					break;
				}
				case MENU_DELETE: {
					Dialog dialog = new AlertDialog.Builder(Main.this)
							.setTitle("删除当前词库")
							.setMessage("确定要将这个词库删除吗？")
							.setPositiveButton("确定",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
											DataAccess data = new DataAccess(
													Main.this);
											data.DeleteBook();
											DataAccess.bookID = "";
											Toast.makeText(Main.this, "该词库已删除",
													Toast.LENGTH_SHORT).show();
										}
									})
							.setNegativeButton("取消",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
										}
									}).create();
					dialog.show();
				}

				}
			}
		});
		//

	}

	private SimpleAdapter getMenuAdapter(String[] menuNameArray,
                                         int[] imageResourceArray) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuNameArray.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", imageResourceArray[i]);
			map.put("itemText", menuNameArray[i]);
			data.add(map);
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
				R.layout.main_item_menu,
				new String[] { "itemImage", "itemText" }, new int[] {
						R.id.item_image, R.id.item_text });
		return simperAdapter;
	}
}